package com.zdlyh.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.dto.CategoryDto;
import com.zdlyh.domain.entity.Category;
import com.zdlyh.enums.AppHttpCodeEnum;
import com.zdlyh.service.CategoryService;
import com.zdlyh.utils.BeanCopyUtils;
import com.zdlyh.utils.ExcelUtil;
import com.zdlyh.utils.WebUtils;
import com.zdlyh.domain.vo.ExcelCategoryVo;
import com.zdlyh.domain.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResponseResult<PageVo> getCategoryPage(Integer pageNum, Integer pageSize, CategoryDto categoryDto){
        return categoryService.getCategoryPage(pageNum,pageSize,categoryDto);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryDto categoryDto){
        return categoryService.addCategory(categoryDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable("id") Long id){
        return categoryService.getCategoryById(id);
    }

    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryDto categoryDto){
        return categoryService.updateCategory(categoryDto);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteCategoryById(@PathVariable("id") Long id){
        return categoryService.deleteCategoryById(id);
    }

    @GetMapping("/listAllCategory")
    public ResponseResult getListAllCategory(){
        return categoryService.getListAllCategory();
    }


    // excel模板下载
    @GetMapping(value = "/template")
    public ResponseResult<String> template(HttpServletResponse response){
        String fileName = "分类数据导入模板";
        try {
            ExcelUtil.export(fileName, null, ExcelCategoryVo.class, response);
        } catch (Exception e) {
            return ResponseResult.errorResult(500,"模板下载失败" + e.getMessage());
        }
        return ResponseResult.okResult("模板下载成功！");
    }

    // excel数据导出
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        // 获取需要导出的数据
        List<Category> categoryList = categoryService.list();
        List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryList, ExcelCategoryVo.class);

        String fileName = "分类数据导出" + System.currentTimeMillis();

        ExcelUtil.export(fileName, excelCategoryVos, ExcelCategoryVo.class, response);

        /*try {
            // 设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);

            // 获取需要导出的数据
            List<Category> categoryVos = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);

            // 把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            // 如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }*/
    }

    // excel批量导入数据
    @PostMapping(value = "/import")
    public ResponseResult<String> importEvents(MultipartFile file){
        try {
            List<?> importList = ExcelUtil.importExcel(file, ExcelCategoryVo.class);

            List<Category> categoryList = BeanCopyUtils.copyBeanList(importList, Category.class);
            categoryService.saveBatch(categoryList);
            return ResponseResult.okResult("数据导入完成");
        } catch (Exception e) {
            return ResponseResult.errorResult(500,"数据导入失败！" + e.getMessage());
        }
    }
}
